<?php
class ModelNewsArticle extends Model {
	public function updateViewed($article_id) {
		$this->db->query("UPDATE " . DB_PREFIX . "news_article SET viewed = (viewed + 1) WHERE article_id = '" . (int)$article_id . "'");
	}

	public function getArticle($article_id){
		$sql  = "SELECT na.article_id, DATE_FORMAT(na.date_added,'%d/%c/%Y %H:%i') AS date_added, nad.title, nad.header, nad.body, nad.meta_description, nad.meta_keyword, nc.category_id";
		$sql .= " FROM " . DB_PREFIX . "news_article na";
		$sql .= " LEFT JOIN " . DB_PREFIX . "news_article_description nad ON (na.article_id = nad.article_id)";
		$sql .= " LEFT JOIN " . DB_PREFIX . "news_category nc ON (na.category_id = nc.category_id)";
		$sql .= " LEFT JOIN " . DB_PREFIX . "news_type nt ON (nc.type_id = nt.type_id)";
		$sql .= " WHERE na.status = '1' ";
		$sql .= " AND na.restaurant_id ='" . (int)$this->config->get('config_restaurant_id') . "'";
		$sql .= " AND nad.language_id ='" . (int)$this->config->get('config_language_id') . "'";
		$sql .= " AND na.article_id = '" . (int)$article_id . "'";

		$query = $this->db->query($sql);

		return $query->row;
	}

	public function getArticles($data = array()) {
		$sql  = " SELECT * FROM " . DB_PREFIX . "news_article na";
		$sql .= " LEFT JOIN " . DB_PREFIX . "news_article_description nad ON (na.article_id = nad.article_id)";
		$sql .= " LEFT JOIN " . DB_PREFIX . "news_category nc ON (na.category_id = nc.category_id)";
		$sql .= " LEFT JOIN " . DB_PREFIX . "news_type nt ON (nc.type_id = nt.type_id)";
		$sql .= " WHERE na.status = '1'";
		$sql .= " AND na.restaurant_id ='" .(int)$this->config->get('config_restaurant_id') . "'";
		$sql .= " AND nad.language_id ='" . (int)$this->config->get('config_language_id') . "'";

		if (isset($data['filter_category_id']) && !is_null($data['filter_category_id'])) {
			$sql .= " AND nc.category_id = '" . (int)$data['filter_category_id']. "'";
		}

		if (isset($data['filter_type_id']) && !is_null($data['filter_type_id'])) {
			$sql .= " AND nt.type_id = '" . (int)$data['filter_type_id']. "'";
		}

		$sort_data = array('nad.title', 'na.date_added', 'na.status', 'na.sort_order');

		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];
		} else {
			$sql .= " ORDER BY na.date_added";
		}

		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " ASC";
		}

		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}

			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}

			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}

		$query = $this->db->query($sql);

		return $query->rows;
	}

	public function getArticleAround($data = array()) {
		$sql  = "SELECT na.article_id, nad.title FROM " . DB_PREFIX . "news_article na";
		$sql .= " LEFT JOIN " . DB_PREFIX . "news_article_description nad ON (na.article_id = nad.article_id)";
		$sql .= " LEFT JOIN " . DB_PREFIX . "news_category nc ON (na.category_id = nc.category_id)";
		$sql .= " WHERE na.status = '1'";
		$sql .= " AND na.restaurant_id ='" .(int)$this->config->get('config_restaurant_id') . "'";
		$sql .= " AND nad.language_id ='" . (int)$this->config->get('config_language_id') . "'";

		if (isset($data['filter_category_id']) && !is_null($data['filter_category_id'])) {
			$sql .= " AND nc.category_id = " . (int)$data['filter_category_id'];
		}
		
		if(isset($data['filter_later']) && $data['filter_later']){
			$sql .= " AND na.article_id > '" . (int)$data['filter_article_id'] . "'";
		} else {
			$sql .= " AND na.article_id < '" . (int)$data['filter_article_id'] . "'";
		}

		$sql .= " ORDER BY na.article_id DESC, na.date_added ASC limit 10";

		$query = $this->db->query($sql);

		return $query->rows;
	}
	
	public function getTotalArticles($data = array()) {
		$sql  = " SELECT Count(*) AS total FROM " . DB_PREFIX . "news_article na";
		$sql .= " LEFT JOIN " . DB_PREFIX . "news_article_description nad ON (na.article_id = nad.article_id)";
		$sql .= " LEFT JOIN " . DB_PREFIX . "news_category nc ON (na.category_id = nc.category_id)";
		$sql .= " LEFT JOIN " . DB_PREFIX . "news_type nt ON (nc.type_id = nt.type_id)";
		$sql .= " WHERE na.status = '1' ";
		$sql .= " AND na.restaurant_id ='" .(int)$this->config->get('config_restaurant_id') . "'";
		$sql .= " AND nad.language_id ='" . (int)$this->config->get('config_language_id') . "'";

		if (isset($data['filter_category_id']) && !is_null($data['filter_category_id'])) {
			$sql .= " AND nc.category_id = '" . (int)$data['filter_category_id']. "'";
		}

		if (isset($data['filter_type_id']) && !is_null($data['filter_type_id'])) {
			$sql .= " AND nt.type_id = '" . (int)$data['filter_type_id']. "'";
		}

		$sort_data = array('nad.title', 'na.date_added', 'na.status', 'na.sort_order');

		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];
		} else {
			$sql .= " ORDER BY na.date_added";
		}

		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " ASC";
		}

		$query = $this->db->query($sql);

		return  $query->rows[0]['total'];
	}
}
?>